<?php

error_reporting(E_ALL ^ E_NOTICE);

// Call NceFormTest::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
    define("PHPUnit_MAIN_METHOD", "NceFormTest::main");
}

require_once "PHPUnit/Framework/TestCase.php";
require_once "PHPUnit/Framework/TestSuite.php";

/* include dependencies for EE */

global $REGX, $PREFS, $DB, $GBL, $IN, $EXT, $FNS, $LOC, $LANG, $SESS, $DSP, $OUT, $BM, $TMPL;

include_once "../src/system/plugins/systempath.php";
//print("SYSTEM PATH: $system_path\n\n");//TMP

if(empty($system_path)) // couldn't read the include file
{
	if (eregi("win", PHP_OS)) 
	{
		$system_path = "../../tr2_msm/dist/system/";
	}
	else
	{
		$system_path = "/var/www/ee/tr2_msm/system/";
	}
}

$tmp = $system_path;

$ext = ".php";
define("EXT", $ext); // needed for the module class to load

$tmp = realpath(".");

print("changing directory to $system_path\n");
chdir($system_path) or print("could not chdir into $system_path\n");
require_once "config.php";

ob_start();
require_once "core/core.system.php";
require_once "core/core.template.php";
ob_end_clean();

$TMPL = new Template();
$DB = new DB($db_config);

//$LANG->fetch_language_file('nce_holidays');
//print_r($PREFS->core_ini);//TMP
//print_r($db_config);//TMP

$system_path = $tmp;
//require_once $system_path."modules/weblog/mod.weblog.php";
//require_once "modules/weblog/mod.weblog.php";
print("changing directory back to $tmp\n");
chdir($tmp) or print("could not chdir into $tmp\n");

// include form plugin

require_once "../src/system/plugins/pi.nce_form.php";
/**
 * Test class for pi.nce_form.php
 */
class NceFormTest extends PHPUnit_Framework_TestCase {	
	
	var $nceForm = null;
	var $example_html = "";
	var $example_phrasebook = "";
	
    public static function main() {
		require_once "PHPUnit2/TextUI/TestRunner.php";

        $suite  = new PHPUnit_Framework_TestSuite("NceFormTest");
        $result = PHPUnit_TextUI_TestRunner::run($suite);
	}
	
	protected function setUp() {
		global $TMPL;
/*
		$TMPL->debugging = true;
		ob_start();
		$TMPL->run_template_engine('ncform', 'example');		
		$TMPL->process_template('ncform', 'example', FALSE, '7');		
		ob_end_clean();
		print($TMPL->tagdata);
*/
		$TMPL->var_single = array
							(
							    'setting name="encoding" value="utf-8"' => 'setting name="encoding" value="utf-8"',
							    'setting name="phrasebook" value="custom_phrasebook.xml"' => 'setting name="phrasebook" value="custom_phrasebook.xml"',
							    'setting name="email_template" value="&#47;includes&#47;custom_mail_template.html"' => 'setting name="email_template" value="&#47;includes&#47;custom_mail_template.html"',
							    'setting name="submitted_template" value="&#47;includes&#47;custom_thankyou_template.html"' => 'setting name="submitted_template" value="&#47;includes&#47;custom_thankyou_template.html"',
							    'setting name="email_sender_address" value="noreply@ncsoft.com"' => 'setting name="email_sender_address" value="noreply@ncsoft.com"',
							    'setting name="email_sender_name" value="NCsoft"' => 'setting name="email_sender_name" value="NCsoft"',
							    'phrase key="error-required-general"' => 'phrase key="error-required-general"',
							    'phrase key="label-firstname"' => 'phrase key="label-firstname"',
							    'phrase key="error-required-number"' => 'phrase key="error-required-number"',
							    'phrase key="label-phone"' => 'phrase key="label-phone"',
							    'phrase key="label-multi-choice1"' => 'phrase key="label-multi-choice1"',
							    'phrase key="label-dropdown1"' => 'phrase key="label-dropdown1"',
							    'phrase key="label-radio1"' => 'phrase key="label-radio1"',
							    'phrase key="label-some-more-text"' => 'phrase key="label-some-more-text"',
							    'phrase key="label-dob"' => 'phrase key="label-dob"',
							    'phrase key="error-invalid-email"' => 'phrase key="error-invalid-email"',
							    'day_element' => 'day_element',
							    'month_element' => 'month_element',
							    'year_element' => 'year_element',
							    'phrase key="error-invalid-filetype"' => 'phrase key="error-invalid-filetype"',
							    'phrase key="error-file-too-big"' => 'phrase key="error-file-too-big"',
							    'phrase key="label-photo"' => 'phrase key="label-photo"',
							    'phrase key="label-email"' => 'phrase key="label-email"',
							    'element name="answer_1"' => Array('name' => 'answer_1'),
							    'element name="answer_2"' => Array('name' => 'answer_2'),
							    'element name="answer_3"' => Array('name' => 'answer_3'),
							    'element' => '',
							    'element name="answer_A"' => Array('name' => 'answer_A'),
							    'element name="answer_B"' => Array('name' => 'answer_B'),
							    'element name="answer_C"' => Array('name' => 'answer_C'),
							    'element value="Submit"' => Array('value' => 'Submit'),
							    'element class="form-textfield"' => Array('class' => 'form-textfield'),
							);
		
		$TMPL->var_pair = array
							(
							    'config' => '',
							    'field name="firstname" type="textfield" validation="required"' => Array('name' => 'firstname','type' => 'textfield','validation' => 'required',),
							    'error validation="required"' => Array('validation' => 'required'),
							    'field name="phone" type="textfield" validation="required,number"' => Array('name' => 'phone','type' => 'textfield','validation' => 'required,number'),
							    'error validation="number"' => Array('validation' => 'number'),
							    'field name="multi_choice_1" type="multiple_choice" validation="required"' => Array('name' => 'multi_choice_1','type' => 'multiple_choice','validation' => 'required'),
							    'field name="dropdown_1" type="dropdown" default="choice_2"' => Array('name' => 'dropdown_1','type' => 'dropdown','default' => 'choice_2'),
								'option value="choice_1"' => Array('value' => 'choice_1'),
								'option value="choice_2"' => Array('value' => 'choice_2'),
								'option value="choice_3"' => Array('value' => 'choice_3'),
							    'field name="radio_1" type="radio" validation="required" default="answer_A"' => Array('name' => 'radio_1', 'type' => 'radio', 'validation' => 'required','default' => 'answer_A'),
							    'field name="some_more_text" type="textarea" rows="30"' => Array('name' => 'some_more_text', 'type' => 'textarea', 'rows' => '30'),
							    'field name="date_of_birth" type="date" start_this_year="yes" validation="age_greater_than:18"' => Array('name' => 'date_of_birth', 'type' => 'date', 'start_this_year' => 'yes', 'validation' => 'age_greater_than:18'),
							    'field name="photo" type="upload" label_phrase="upload" validation="types:jpg|jpeg,size:1024"' => Array('name' => 'photo','type' => 'upload','label_phrase' => 'upload','validation' => 'types:jpg|jpeg,size:1024'),
							    'error validation="types"' => Array('validation' => 'types'),
							    'error validation="size"' => Array('validation' => 'size'),							    'field name="email" type="textfield" validation="email"' => Array('name' => 'email','type' => 'textfield','validation' => 'email'),
							    'field name="submit" type="submit" class="form-submit"' => Array('name' => 'submit','type' => 'submit','class' => 'form-submit'),
							);		
		
		$this->example_html = file_get_contents("../templates/ncform/example.php");
		preg_match("'(\{exp:nce_form.+\{/exp:nce_form[^\}]+\})'siU", $this->example_html, $matches);
		$this->example_html = $matches[1];
		$this->example_html = str_replace("/", "&#47;", $this->example_html);
		//print("[".$this->example_html."]");//TMP
		
		$this->example_phrasebook = file_get_contents("../templates/ncform/nce_form_phrasebook.xml");
		//print("[".$this->example_phrasebook."]");//TMP
		
		$this->nceForm = new Nce_form();
	}
	
	protected function tearDown() {
		$this->nceForm = null;
	}

	function test_ncform_createtagobjects()
	{
	}
	
	function test_dummy()
	{
		$this->assertTrue(true, "dummy test failed");
	}
	
	/*************** tests for phrasebook ****************/

	function test_load_phrasebook_1()
	{
		$lang = "en";
		
		$this->nceForm->load_phrasebooks($lang, $this->example_phrasebook);
		$phrasebook = $this->nceForm->phrasebook;

		$this->assertNotNull($phrasebook, "Standard phrasebook not loaded");
	}

	function test_load_phrasebook_2()
	{
		$lang = "en";
		
		$this->nceForm->load_phrasebooks($lang, $this->example_phrasebook);
		$phrasebook = $this->nceForm->phrasebook;
		$count = count($phrasebook);
		
		$this->assertNotEquals(0, $count, "Standard english phrasebook empty");
	}
	
	function test_replaced_phrases_1()
	{
		$lang = "en";
		$html = '<div>{phrase key="error-required-general"}</div>';
		$expected = "<div>You must enter a value</div>";
		
		$this->nceForm->load_phrasebooks($lang, $this->example_phrasebook);
		$result = $this->nceForm->get_replaced_phrases($html);

		$this->assertEquals($expected, $result, "Replaced phrase is not correct");
	}
	
	function test_replaced_phrases_2()
	{	
		$lang = "en";
		$html = '<div>{phrase key="error-required-general"}</div><div>{phrase key="error-invalid-email"}</div>';
		$expected = "<div>You must enter a value</div><div>You must enter a valid email address</div>";
		
		$this->nceForm->load_phrasebooks($lang, $this->example_phrasebook);
		$result = $this->nceForm->get_replaced_phrases($html);

		$this->assertEquals($expected, $result, "Replaced phrase is not correct");
	}
	
	function test_replaced_phrases_3()
	{
		$lang = "en";
		$html = '<div>{phrase key="error-required-general"}</div><div>{phrase key="error-required-general"}</div><div>{phrase key="error-required-general"}</div>';
		$expected = "<div>You must enter a value</div><div>You must enter a value</div><div>You must enter a value</div>";
		
		$this->nceForm->load_phrasebooks($lang, $this->example_phrasebook);
		$result = $this->nceForm->get_replaced_phrases($html);

		$this->assertEquals($expected, $result, "Replaced phrase is not correct");
	}
	
	function test_replaced_phrases_4()
	{	
		$lang = "de";
		$html = '<div>{phrase key="label-dob"}</div>';
		$expected = "<div>Date of birth DE</div>";
		
		$this->nceForm->load_phrasebooks($lang, $this->example_phrasebook);
		$result = $this->nceForm->get_replaced_phrases($html);

		$this->assertEquals($expected, $result, "Replaced phrase is not correct");
	}
	
	/*********** tests for TagObject *******************/
	
	function test_tag_object_1()
	{
		$params = array();
		$tag_object = new TagObject('whatever', $params);
	
		$this->assertNotNull($tag_object);
	}
	
	function test_tag_object_init_params()
	{	
		global $TMPL;
	
		$params = 'name="encoding" value="utf-8"';
		$tag_object = new TagObject("setting", array());
		$tag_object->init_params($params);
		$this->assertEquals($tag_object->params['name'], 'encoding', "params not loaded");
		$this->assertEquals($tag_object->params['value'], 'utf-8', "params not loaded");
	}
	
	function test_tag_object_create_tags1()
	{	
		global $TMPL;
		$TMPL->tagdata = $this->example_html;
		
		$tags = TagObject::create_tag_objects($TMPL->tagdata, 'config');
//		print("tags:\n");//TMP
//		print_r($tags);//TMP
		$config = $tags[0];
		$this->assertNotNull($config);
		$this->assertEquals($config->name, 'config');
	}
	
	function test_create_inner_tag_objects1()
	{
		global $TMPL;
		$TMPL->tagdata = $this->example_html;
		
		$tags = TagObject::create_tag_objects($TMPL->tagdata, 'config');
		$config = $tags[0];
		$setting_tags = $config->create_inner_tag_objects('setting');
//		print("tags:\n");//TMP
//		print_r($tags);//TMP
		$this->assertNotNull($setting_tags);
		$this->assertEquals($setting_tags[0]->name, 'setting');	
	}
	
	function test_single_params_to_string1()
	{
		global $TMPL;
	
		$params = 'name="encoding" value="utf-8"';
		$tag_object = new TagObject("setting", array());
		$tag_object->init_params($params);
		$param_string = $tag_object->single_params_to_string();
		$this->assertEquals($param_string, $params, "params [$params] not the same as [$param_string]");
	}
	function test_create_config()
	{
		global $TMPL;
		$TMPL->tagdata = $this->example_html;
		
		$this->nceForm->load_form_config();
		$config = $this->nceForm->config;

		$this->assertEquals("utf-8", $config['encoding']);
		
	}
	
	/***************************** tests for emailing ****************************/
	function test_create_email_sender()
	{	
		$mail = new EmailSender("emartin@ncsoft.com", "emartin");
		$this->assertNotNull($mail);
		$this->assertEquals($mail->sender_address, "emartin@ncsoft.com");
		$this->assertEquals($mail->sender_name, "emartin");
	}
	
	function test_get_email_body()
	{
		$mail = new EmailSender("output", "output");
		
		$body_text = $mail->get_body_text();
		//should return nothing in the body text now.
		$this->assertTrue(empty($body_text));
		//Pass in the body text
		$mail->body_text = "random body text";
		$this->assertEquals("random body text", $mail->get_body_text());
		//Pass in a template name and group to test. 
		$mail->template_name = "Name";
		$mail->template_group = "_components";
		//Should fail, so should still get the same pre-passed body text? 
		//$this->assertEquals("random body text", $mail->get_body_text());
		//And finally should test returning the template.
	}
	
	function test_add_email_recipients()
	{
		$mail = new EmailSender("output", "output");
		$mail->addRecipient("recipient address", "recipient name");
		
		$this->assertEquals("recipient address", $mail->recipient_address);
		$this->assertEquals("recipient name", $mail->recipient_name);
		
	}
	/*
	function test_send_email()
	{
		$mail = new EmailSender("output", "output");
		$mail->addRecipient("emartin@ncsoft.com", "Ellie");
		
		$mail->email_subject = "Test email sending";
		$mail->body_text = "This email was a test testing nce_email.";
		
		$this->assertTrue($mail->sendMail());
	}
	*/
	
	function test_get_email_settings()
	{
		global $TMPL;
		$TMPL->tagdata = $this->example_html;
		
		$this->nceForm->load_form_config();
		$config = $this->nceForm->config;
		
		//$this->assertEquals('/includes/custom_mail_template.html', $config['email_template']);
		$this->assertEquals("noreply@ncsoft.com", $config['email_sender_address']);
		$this->assertEquals("NCsoft", $config['email_sender_name']);
	}
	/***************************** end tests for emailing ****************************/
	
	
	/*********** tests for validation *******************/
	
	function test_is_valid_number_1()
	{
		$value = 29;
		$result = FieldObject::is_valid_number($value);
		
		$this->assertTrue($result, 'Number not recognised as number');
	}
	
	function test_is_valid_number_2()
	{
		$value = 'grrrrr arggg';
		$result = FieldObject::is_valid_number($value);
		
		$this->assertFalse($result, 'Text "'.$value.'" recognised as a number');
	}
	
	function test_is_valid_email_1()
	{
		$value = 'tdallaway@ncsoft.com';
		$result = FieldObject::is_valid_email($value);
		
		$this->assertTrue($result);
	}
	
	function test_is_valid_email_2()
	{
		$value = 'afgaeefgsdfgasgdasfd';
		$result = FieldObject::is_valid_email($value);
		
		$this->assertFalse($result);
	}

	function test_is_valid_email_3()
	{
		$value = 'afgaeefgsdfgasgdasfd@dsdsds';
		$result = FieldObject::is_valid_email($value);
		
		$this->assertFalse($result);
	}

	function test_is_valid_email_4()
	{
		$value = 'a@dsdsds.sddsd';
		$result = FieldObject::is_valid_email($value);
		
		$this->assertTrue($result);
	}

	function test_is_valid_email_5()
	{
		$value = 't�dll�w�y@ncs�ft.c�m';
		$result = FieldObject::is_valid_email($value);
		
		$this->assertTrue($result);
	}

	function test_is_valid_email_6()
	{
		$value = 't�dl-l�w�y@ncs_�ft.c�m';
		$result = FieldObject::is_valid_email($value);
		
		$this->assertTrue($result);
	}
	
	function test_is_valid_email_7()
	{
		$value = 'tdallaway@ ncsoft.com';
		$result = FieldObject::is_valid_email($value);
		
		$this->assertFalse($result);
	}
	
	
	function test_is_valid_age_1()
	{
		$min_age = 18;
		$now = time();
		$day = date('d', $now);
		$month = date('m', $now);
		$year = date('Y', $now)-18;
		
		$result = FieldObject::is_valid_age($min_age, $year, $month, $day);
		
		$this->assertTrue($result);
	}
	
	function test_is_valid_age_2()
	{
		$min_age = 18;
		$now = time();
		$day = date('d', $now);
		$month = date('m', $now);
		$year = date('Y', $now)-17;
		
		$result = FieldObject::is_valid_age($min_age, $year, $month, $day);
		
		$this->assertFalse($result);
	}
	
	function test_is_valid_age_3()
	{
		$min_age = 18;
		$now = time();
		$day = date('d', $now)+1;
		$month = date('m', $now);
		$year = date('Y', $now)-18;
		
		$result = FieldObject::is_valid_age($min_age, $year, $month, $day);
		
		$this->assertFalse($result);
	}
}
?>
